{% extends "main_card.html.twig" %}

{% import "info_providers/providers.macro.html.twig" as providers_macro %}
{% import "helper.twig" as helper %}

{% block title %}
    {% if update_target %}
        {% trans %}info_providers.update_part.title{% endtrans %}
    {% else %}
        {% trans %}info_providers.search.title{% endtrans %}
    {% endif %}
{% endblock %}

{% block card_title %}
    {% if update_target %} {# If update_target is set, we update an existing part #}
        <i class="fas fa-cloud-arrow-down"></i> {% trans %}info_providers.update_part.title{% endtrans %}:
        <b><a href="{{ entity_url(update_target) }}" target="_blank" class="text-bg-primary">{{ update_target.name }}</a></b>
    {% else %} {# Create a fresh part #}
        <i class="fas fa-cloud-arrow-down"></i> {% trans %}info_providers.search.title{% endtrans %}
    {% endif %}
{% endblock %}

{% block card_content %}

    {{ form_start(form) }}

    {{ form_row(form.keyword) }}
    {{ form_row(form.providers) }}

    <div class="row mb-2">
        <div class="col-sm-9 offset-sm-3">
            <a href="{{ path('info_providers_list') }}">{% trans %}info_providers.search.info_providers_list{% endtrans %}</a>
        </div>
    </div>

    {{ form_row(form.submit) }}

    {{ form_end(form) }}

    {% if results is not null %}

        {% if results|length > 0 %}
            <b>{% trans with {'%number%': results|length} %}info_providers.search.number_of_results{% endtrans %}</b>:

            <table class="table table-striped table-hover">
                <thead>
                <tr>
                    <th></th>
                    <th>{% trans %}name.label{% endtrans %} / {% trans %}part.table.mpn{% endtrans %}</th>
                    <th>{% trans %}description.label{% endtrans %} / {% trans %}category.label{% endtrans %}</th>
                    <th>{% trans %}manufacturer.label{% endtrans %} / {% trans %}footprint.label{% endtrans %}</th>
                    <th>{% trans %}part.table.manufacturingStatus{% endtrans %}</th>
                    <th>{% trans %}info_providers.table.provider.label{% endtrans %}</th>
                    <th></th>
                </tr>
                </thead>
                <tbody>
                {% for result in results  %}
                    {% set dto = result["dto"] %}
                    {# @var App\Entity\Parts\Part localPart #}
                    {% set localPart = result["localPart"] %}

                    <tr {% if localPart is not null %}class="table-warning"{% endif %}>
                        <td>
                            <img src="{{ dto.preview_image_url }}" data-thumbnail="{{ dto.preview_image_url }}"
                                 class="hoverpic" style="max-width: 45px;" {{ stimulus_controller('elements/hoverpic') }}>
                        </td>
                        <td>
                            {% if dto.provider_url is not null %}
                                <a href="{{ dto.provider_url }}" target="_blank" rel="noopener">{{ dto.name }}</a>
                            {% else %}
                                {{ dto.name }}
                            {% endif %}

                            {% if dto.mpn is not null %}
                                <br>
                                <small class="text-muted" title="{% trans %}part.table.mpn{% endtrans %}">{{ dto.mpn }}</small>
                            {% endif %}
                            {%  if result["localPart"] is not null %}

                            {% endif %}
                        </td>
                        <td>
                            {{ dto.description }}
                            {% if dto.category is not null %}
                                <br>
                                <small class="text-muted">{{ dto.category }}</small>
                            {% endif %}
                        </td>
                        <td>
                            {{ dto.manufacturer ?? '' }}
                            {% if dto.footprint is not null %}
                                <br>
                                <small class="text-muted">{{ dto.footprint }}</small>
                            {% endif %}
                        </td>
                        <td>{{ helper.m_status_to_badge(dto.manufacturing_status) }}</td>
                        <td>
                            {% if dto.provider_url %}
                                <a href="{{ dto.provider_url }}" target="_blank" rel="noopener">
                                    {{ info_provider_label(dto.provider_key)|default(dto.provider_key) }}
                                </a>
                            {% else %}
                                {{ info_provider_label(dto.provider_key)|default(dto.provider_key) }}
                            {% endif %}
                            <br>
                            <small class="text-muted">{{ dto.provider_id }}</small>
                        </td>
                        <td class="text-center">

                            {% if update_target %} {# We update an existing part #}
                                {% set href = path('info_providers_update_part',
                                    {'providerKey': dto.provider_key, 'providerId': dto.provider_id, 'id': update_target.iD}) %}
                            {% else %} {# Create a fresh part #}
                                {% set href = path('info_providers_create_part',
                                    {'providerKey': dto.provider_key, 'providerId': dto.provider_id}) %}
                            {% endif %}

                            {# If we have no local part, then we can just show the create button #}
                            {% if localPart is null %}
                                <a class="btn btn-primary" href="{{ href }}"
                                target="_blank" title="{% trans %}part.create.btn{% endtrans %}">
                                    <i class="fa-solid fa-plus-square"></i>
                                </a>
                            {% else %} {#  Otherwise add a button group with all three buttons #}
                                <span class="badge text-bg-warning mb-1 d-block" title="{% trans %}info_providers.search.existing_part_found{% endtrans %}">
                                    <i class="fa-solid fa-circle-info fa-fw"></i>
                                    {% trans %}info_providers.search.existing_part_found.short{% endtrans %}
                                </span>

                                <div class="btn-group" role="group">
                                    <a class="btn btn-primary" href="{{ path('app_part_show', {'id': localPart.id}) }}"
                                       target="_blank" title="{% trans %}info_providers.search.show_existing_part{% endtrans %}">
                                        <i class="fa-solid fa-search"></i>
                                    </a>
                                    <a class="btn btn-primary" href="{{ path("info_providers_update_part", {'id': localPart.id, 'providerKey': dto.provider_key, 'providerId': dto.provider_id}) }}"
                                       target="_blank" title="{% trans %}info_providers.search.update_existing_part{% endtrans %}">
                                        <i class="fa-solid fa-arrows-rotate"></i>
                                    </a>
                                    <div class="btn-group" role="group">
                                        <button type="button" class="btn btn-primary dropdown-toggle" data-bs-toggle="dropdown" aria-expanded="false"></button>

                                        <ul class="dropdown-menu">
                                            <li><a class="dropdown-item" href="{{ path('part_edit', {'id': localPart.id}) }}" target="_blank">
                                                    <i class="fa-solid fa-pencil fa-fw"></i> {% trans %}info_providers.search.edit_existing_part{% endtrans %}
                                                </a></li>
                                            <li>
                                                <a class="dropdown-item" href="{{ href }}" target="_blank">
                                                    <i class="fa-solid fa-plus-square fa-fw"></i> {% trans %}part.create.btn{% endtrans %}
                                                </a>
                                            </li>
                                        </ul>
                                    </div>
                                </div>

                            {% endif %}
                        </td>
                    </tr>
                {% endfor %}

                </tbody>
            </table>
        {% else %}
            <div class="alert alert-info" role="alert">
                {% trans %}info_providers.search.no_results{% endtrans %}
            </div>
        {% endif %}
    {% endif %}

{% endblock %}
